require('Contact');
var Game = require('Game');
cc.Class({
    extends: cc.Component,

    properties: {

    },
    // use this for initialization
    onLoad: function () {
        var self = this;
        this.firstContract = false;
        this.speed = Game.instance.speed;
        this.alive = true;
        // 旋转当中计算真实的可视高度
        this.viewHigh = this.node.height;
        // 物理组件相关
        this.physicsCubeBody = this.getComponent(Body);
        this.physicsCubeBody.bodyType = 2;
        this.downWallHeight = Game.instance.wallDownNode.height/2;
        //给一个初始的冲量，直接给物体速度
        this.physicsCubeBody.body.ApplyImpulse(new b2Vec2(0, -this.speed),this.physicsCubeBody.body.GetWorldCenter());
        this.physicsCubeBody.addContactEvent(ContactType.BEGIN_CONTACT,function(){
             if(!self.firstContract){ 
                self.physicsCubeBody.bodyType = 2;
                self.firstContract = true;
                var currentMeter = self.getHighMeter();
                if(currentMeter > Game.instance.highMeter){
                    Game.instance.updateHighMeter(currentMeter);
                    
                }
                // 如果当前游戏最高点显示在大于屏幕的1/3位置，屏幕（gameView）下移
                if(currentMeter+Game.instance.gameArea.y > Game.instance.gameArea.height / 3){
                    //console.log(self.node.y+" "+currentMeter+" "+Game.instance.gameArea.y);
                    Game.instance.moveBg(currentMeter+Game.instance.gameArea.y - Game.instance.gameArea.height / 3);
                }
            }
        });
    },
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if(!this.firstContract){
            //碰撞之前持续给出反重力使节点匀速运动
            this.physicsCubeBody.body.ApplyForce(new b2Vec2(0, 9.8*this.physicsCubeBody.body.GetMass()),this.physicsCubeBody.body.GetWorldCenter());
        }
        // 出左右边界后销毁并触发新节点
        if(this.node.getBoundingBox().xMax<Game.instance.leftBorder || this.node.getBoundingBox().xMin>Game.instance.rightBorder){
            if(this.node.uuid === Game.instance.physicsControlCube.uuid){
                Game.instance.generateCube();
            }
            this.node.destroy();
            Game.instance.subLife(1);
            //console.log("destroyed");
        }
        // 出下边界并不销毁，只是触发新节点
        // 如果平台在下边界小于游戏宽度，则还需要判断节点掉到水平面之下进行扣减生命和销毁节点
        if(this.node.getBoundingBox().yMax+Game.instance.gameArea.y<Game.instance.bottomBorder){
            if(this.node.uuid === Game.instance.physicsControlCube.uuid){
                Game.instance.generateCube();
                Game.instance.subLife(1);
            }
        }
    },
    onLeft: function(delta){
        if(!this.firstContract){
            this.node.x -= delta;
            // 判断撞墙，兼容旋转后的情况
            if(this.node.getBoundingBox().xMin < 0 - Game.instance.gameArea.width/2){
                this.node.x += delta;
            }
        }
    },
    onRight: function(delta){
        if(!this.firstContract){
            this.node.x += delta;
            if(this.node.getBoundingBox().xMax > Game.instance.gameArea.width/2){
                this.node.x -= delta;
            }
        }
    },
    onChangeDirection : function(){
        if(!this.firstContract){
            var action = cc.rotateBy(0,90);
            this.node.runAction(action);
            // 调整高和宽
            if(this.viewHigh === this.node.height){
                this.viewHigh = this.node.width;
            }else{
                this.viewHigh = this.node.height;
            }
        }
    },
    onRapidDownStart : function(){
        if(!this.firstContract){
            // 冲量是叠加的，需要先抵消掉原有冲量
            this.physicsCubeBody.body.ApplyImpulse(new b2Vec2(0, this.speed),this.physicsCubeBody.body.GetWorldCenter());
            this.speed = 80;
            this.physicsCubeBody.body.ApplyImpulse(new b2Vec2(0, -this.speed),this.physicsCubeBody.body.GetWorldCenter());
        }
    },
    onRapidDownEnd : function(){
        if(!this.firstContract){
            this.physicsCubeBody.body.ApplyImpulse(new b2Vec2(0, this.speed),this.physicsCubeBody.body.GetWorldCenter());
            this.speed = 30;
            this.physicsCubeBody.body.ApplyImpulse(new b2Vec2(0, -this.speed),this.physicsCubeBody.body.GetWorldCenter());
        }
    },
    /**
     * 根据参数计算cube的最高点的值
     * gameViewHeight：游戏区域高度（正数）
     * cubeHighY：节点Y值，如果在半屏以下，为负数
     * platFormY2：底部碰撞平台的高度的一半
     * gameViewY：游戏背景区域移动的距离,负数
     */
    getHighMeterWithParam:function(gameViewHeight,cubeHighY,platFormY2,gameViewY){
        return Math.floor(gameViewHeight/2+cubeHighY-platFormY2);
    },
    getHighMeter:function(){
        return this.getHighMeterWithParam(Game.instance.gameArea.height,this.node.getBoundingBox().yMax,this.downWallHeight);
    }
});
